package bsf.baseservice.businessmq.redis;

import bsf.base.CallBack;
import bsf.base.TimeSpan;
import bsf.baseservice.businessmq.redis.messagelock.BaseMessageLock;
import bsf.baseservice.businessmq.redis.messagelock.SendMessageLock;
import bsf.baseservice.businessmq.systemruntime.BusinessMQException;
import bsf.baseservice.businessmq.systemruntime.SystemParamConfig;
import bsf.redis.RedisDb;
import bsf.redis.RedisManager;

/**
 redis网络命令发送
 
*/
public class RedisNetCommand
{
	private String redisServerIp;
	private Integer redisport;
	private String redisauth;
	private BaseMessageLock messagelock = new SendMessageLock(new TimeSpan(500));

	public RedisNetCommand(String rediserverip)
	{
		/*redisServerIp = rediserverip;兼容C#的redis连接写法*/
		try
		{
			if (rediserverip.indexOf('@') >= 0) {
				redisauth = rediserverip.substring(0, rediserverip.indexOf('@'));
			}
			if (rediserverip.indexOf(':') > 0) {
				redisport = Integer.parseInt(rediserverip.substring(rediserverip.indexOf(':')+1, rediserverip.length()));
			}
			redisServerIp = rediserverip.replace(":" + redisport, "").replace(redisauth + "@", "");
		}
		catch (Exception exp)
		{
			throw new BusinessMQException("reids连接配置错误,格式应为auth@ip:port");
		}
	}
	/** 
	 可丢消息发送,当前消息并发情况下丢弃
	 
	 @param mqpath
	*/
	public final void sendMessage(final String mqpath)
	{
		messagelock.lock(new CallBack.Func0() {
			@Override
			public Object invoke() {
				try
				{
					RedisManager manager = new RedisManager();
					try(RedisDb c = manager.getPoolClient(redisServerIp,redisport,redisauth, SystemParamConfig.Redis_MaxConnectPoolSize, 1)) {
						long i = c.getClient().publish(SystemParamConfig.Redis_Channel_Quque + "." + mqpath, "1");
					}
				}
				catch (Exception exp)
				{
				}
				return null;
			}
		});
	}


}